//
//  YW_NetworkManager.m
//  ZKChargingPile
//
//  Created by 陈友文 on 2021/1/22.
//  Copyright © 2021 陈友文. All rights reserved.
//

#import "YW_NetworkManager.h"

#define k_timeOunt 30

#if DEBUG
#define k_baseURLString @"https://xcdz.sensorte.com/"
#else
#define k_baseURLString @"https://xcdz.sensorte.com/"

#endif


@interface YW_NetworkManager ()

@property (nonatomic,assign) AFNetworkReachabilityStatus reachabilityStatus;

@end


@implementation YW_NetworkManager



+(instancetype)shareManager{
    
    static dispatch_once_t onceToken;
    static YW_NetworkManager *manager = nil;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] initWithBaseURL:[NSURL URLWithString:k_baseURLString]];
        [manager settingAFNetworking];
    });
    
    return manager;
}


-(void)yw_networkType:(YWNetWorkType)type
            urlString:(NSString *)URLString
           parameters:(id)parameters
               header:(nullable NSDictionary <NSString *, NSString *> *)headers
           completion:(yw_completionBlock)completionBlock{
    

    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        self.reachabilityStatus = status;
    }];
    
    yw_successBlock successBlock = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completionBlock(responseObject,YES,self.reachabilityStatus);
    };
    
    yw_failBlock failBlock =^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        completionBlock(error,NO,self.reachabilityStatus);
    };


    if (type == YWNetWorkTypeGET) {//GET请求
        
        [self GET:URLString parameters:parameters headers:headers progress:nil success:successBlock failure:failBlock];
        
    }else if (type == YWNetWorkTypePOST){//POST请求
        
        [self POST:URLString parameters:parameters headers:headers progress:nil success:successBlock failure:failBlock];
        
    }else if (type == YWNetWorkTypePUT){//PUT请求
        
        [self PUT:URLString parameters:parameters headers:headers success:successBlock failure:failBlock];
        
    }else if (type == YWNetWorkTypeDELETE){//DELETE请求
        
        [self DELETE:URLString parameters:parameters headers:headers success:successBlock failure:failBlock];

    }
    
}


-(void)yw_upImageUrl:(NSString *)URLString
           parameter:(NSDictionary *)parameter
            imageArr:(NSArray *)imageArr
              header:(nullable NSDictionary <NSString *, NSString *> *)headers
          completion:(yw_completionBlock)completionBlock{
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        self.reachabilityStatus = status;
    }];
    yw_successBlock successBlock = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completionBlock(responseObject,YES,self.reachabilityStatus);
    };
    
    yw_failBlock failBlock = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completionBlock(error,NO,self.reachabilityStatus);
    };

    [self POST:URLString parameters:parameter headers:headers constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (NSInteger i = 0; i < imageArr.count; i++) {
            NSData *imageData = UIImageJPEGRepresentation(imageArr[i], 0.3f);
            [formData appendPartWithFileData:imageData name:@"file" fileName:[NSString stringWithFormat:@"image%ld.jpg",i] mimeType:@"image/jpg"];
        }
    } progress:nil success:successBlock failure:failBlock];
    
}

-(void)settingAFNetworking{
    
    self.requestSerializer = [AFJSONRequestSerializer serializer];
    [self.requestSerializer setTimeoutInterval:k_timeOunt];
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain",@"application/x-www-form-urlencoded", nil];
}







@end
